﻿using Common.Extensions;
using Common.Utils;
using GYMm.UI.Controls;
using GYMm.UI.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace GYMm.UI.Views
{
	/// <summary>
	/// An empty page that can be used on its own or navigated to within a Frame.
	/// </summary>
	public sealed partial class ExerciseView : Page
	{
		private int _lastScrollingDirection;
		private ExerciseViewModel ViewModel
		{
			get
			{
				return DataContext as ExerciseViewModel;
			}
		}

		public ExerciseView()
		{
			this.InitializeComponent();
			Bars.Clicked = new GuidRelayCommand(OnBarClicked);
            this.ManipulationMode = ManipulationModes.TranslateX;
            RepsInput.ManipulationMode = ManipulationModes.TranslateY | ManipulationModes.TranslateInertia;
            WeightInput.ManipulationMode = ManipulationModes.TranslateY | ManipulationModes.TranslateInertia;
            RepsInput.ManipulationDelta += RepsInput_ManipulationDelta;
            WeightInput.ManipulationDelta += RepsInput_ManipulationDelta;
            RepsInput.ManipulationCompleted += RepsInput_ManipulationCompleted;
            WeightInput.ManipulationCompleted += RepsInput_ManipulationCompleted;
		}

        void RepsInput_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            _yDeltaAcc = 0.0;
        }

        private Double _yDeltaAcc = 0.0;
        void RepsInput_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            var input = sender as Input;

            _yDeltaAcc += e.Delta.Translation.Y;

            if (Math.Abs(_yDeltaAcc) > 100)
            {
                var newVal = _yDeltaAcc > 0.0 ? input.Value - input.Step : input.Value + input.Step;
                input.Value = Math.Max(0.0, newVal);

                _yDeltaAcc = 0.0;
            }            
        }

		private void OnBarClicked(Guid id)
		{            
			ViewModel.ShowAddSetLogDialog(id);
            LogBtn.Flyout.ShowAt(this);
		}

		private async void OnSaveClick(object sender, RoutedEventArgs e)
		{
            LogBtn.Flyout.Hide();
			await ViewModel.SaveSetLog(WeightInput.Value, (Int32)RepsInput.Value);
		}

		public static Double ToDouble(String str)
		{
			double res;
			Double.TryParse(str, out res);
			return res;
		}

		public static Int32 ToInt(String str)
		{
			Int32 res;
			Int32.TryParse(str, out res);
			return res;
		}

		private async void OnRemoveClick(object sender, RoutedEventArgs e)
		{
			await ViewModel.RemoveSelectedSetLogAsync();
		}

		private void OnNewClick(object sender, RoutedEventArgs e)
		{
			ViewModel.ShowAddSetLogDialog();
		}

		private void OnCloseClick(object sender, RoutedEventArgs e)
		{
            LogBtn.Flyout.Hide();
			ViewModel.HideAddSetLogDialog();            
		}

		protected override void OnManipulationCompleted(ManipulationCompletedRoutedEventArgs e)
		{
			var tmp = _lastScrollingDirection;
			_lastScrollingDirection = 0;

			if (tmp < 0) //swipe to the left
				ViewModel.GoToNext();
			else if (tmp > 0) //swipe to the right
				ViewModel.GoToPrev();

			base.OnManipulationCompleted(e);
		}

		protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e)
		{
			_lastScrollingDirection = Math.Sign(e.Cumulative.Translation.X);            

			base.OnManipulationDelta(e);
		}

		private void RadioButton_Checked(object sender, RoutedEventArgs e)
		{
			if (WeightInput == null)
				return;

			var rb = sender as RadioButton;
			if (!rb.IsChecked.HasValue || !rb.IsChecked.Value)
				return;

			var content = (String)rb.Content;
			WeightInput.Step = content.ToDouble(2.5);
		}

        private void DialogBackTapped(object sender, TappedRoutedEventArgs e)
        {
            LogBtn.Flyout.Hide();
            ViewModel.HideAddSetLogDialog();
        }        
	}
}
